<?php
class adminNavMod extends commonMod
{
	public function __construct()
	{
		parent::__construct();
	}
	//导航列表
	public function get_list()
	{
		
		$order=in(isset($_GET[0]) ? trim($_GET[0]) :'show_order');
		//分页
		$url=__URL__.'/get_list-'.$order.'-page-{page}.html';
		$count=$this->model->table('admin_nav')->count();
		$list_rows=10;
		$page=new Page();
		$cur_page=$page->getCurPage();
		$this->assign('page',$this->page($url,$count,$list_rows));
		$this->assign('list',$this->condition_list($list_rows,$cur_page,'',$order));
		$this->assign('lang',LANG::getPack('adminNav'));
		$this->display('admin_nav/list.html');
	}
	//添加
	public function add()
	{
		if(empty($_POST['do']))
		{
			$parent_id=intval($_GET[0]);
			$this->assign('parent_id',$parent_id);
			$this->assign('list',$this->condition_list('1000','1','','show_order'));
			$this->assign('lang',LANG::getPack('adminNav'));
			$this->display('admin_nav/add.html');
			return;
			}
		$data=array();
		$data['name']=in($_POST['name']);
		$data['parent_id']=in($_POST['parent_id']);
		$data['url']=in($_POST['url']);
		$data['show_order']=in($_POST['show_order']);
		if(empty($data['name']))
		{
			$this->error('后台导航名称不可为空！');
			}
		if($this->model->table('admin_nav')->data($data)->insert())
		{
			$this->success('添加成功！','/get_list');
			}
		else
		{
			$this->error('添加失败！');
			}
		}
	//编辑
	public function edit()
	{
		if(empty($_POST['do']))
		{
			$id=intval($_GET[0]);
			$this->assign('read',$this->read($id));
			$this->assign('list',$this->condition_list('1000','1','','show_order'));
			$this->assign('lang',LANG::getPack('adminNav'));
			$this->display('admin_nav/edit.html');
			return;
			}
		$data=array();
		$data['name']=in($_POST['name']);
		$data['parent_id']=in($_POST['parent_id']);
		$data['url']=in($_POST['url']);
		$data['show_order']=in($_POST['show_order']);
		if(empty($data['name']))
		{
			$this->error('后台导航名称不可为空！');
			}
		$condition['id']=intval($_POST['id']);
		if($this->model->table('admin_nav')->data($data)->where($condition)->update())
		{
			$this->success('修改成功！','/get_list');
			}
		else
		{
			$this->error('修改失败！或您未修改任何数据！');
			}
		
		}
	//读取
	public function read($id)
	{
		$condition['id']=$id;
		$read=$this->model->table('admin_nav')->where($condition)->find();
		if($read)
		{
			return $read;
			}
		else
		{
			$this->error('不存在此信息！');
			}
		}
	//删除
	public function del()
	{
		$condition['id']=intval($_GET[0]);
		if($this->model->table('admin_nav')->where($condition)->delete())
		{
			$this->success('删除成功！','/get_list');
			}
		else
		{
			$this->error('删除失败！');
			}
		}
	//查询列表
	public function condition_list($list_rows,$cur_page,$where,$order)
	{
		$limit_start=($cur_page-1)*$list_rows;
		$data=$this->model->field('id,parent_id,name,url,show_order')->table('admin_nav')->where($where)->order($order.' DESC')->select();
		$cat=new Category(array('id','parent_id','name'));
		$list=$cat->getTree($data,'0');
		if($list)
		{
			foreach($list as $key=>$val)
			{
				if($key<($limit_start+$list_rows)&&$key>=$limit_start)
				{
					$list[$key]=$val;
				}
				else{
					unset($list[$key]);
				}
				}
			return $list;
			}
		}
	
	//删除所选
	public function alldel()
	{
		$id=$_POST['id'];
		$new_id = substr($id,0,strlen($id)-1); 
		
		if($this->model->table('admin_nav')->where('id in ('.$new_id.')')->delete())
		{
			echo 1;
			}
		else
		{
			echo 0;
			}
		}
	
	//更新排序
	public function set()
	{
		$id=explode(',',substr($_POST['id'],0,strlen($_POST['id'])-1));
		$order=explode(',',substr($_POST['order'],0,strlen($_POST['order'])-1));
		$num=0;
		for($i=0;$i<count($id);$i++)
		{
			$condition['id']=$id[$i];
			if($this->model->table('admin_nav')->data("show_order='".$order[$i]."'")->where($condition)->update());
			{
				$num++;
				}
			}
		if(count($id)==$num)
		{
			echo "1";
			}
		else
		{
			echo "0";
			}
	}
}
?>